<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>

	<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.min.css"/>
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.min.js"></script>
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteConts()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="contentShow">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<form id="page">

								<a href="javascript:toPage(1)">首页</a>
								<a href="javascript:toPage(2)">上一页</a>
								第<input type="text"  id="pageNum" size="1px" readonly="readonly">页/
								共<input type="text" id="pages" size="1px" disabled="disabled">页/
								共<input type="text" id="total" size="1px" disabled="disabled">条
								<a href="javascript:toPage(3)">下一页</a>
								<a href="javascript:toPage(4)">尾页</a>
							</form>
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口zengja -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			<input type="hidden" id="pic">
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select id="cateShow" class="form-control" >
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input id="title"  class="form-control" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input id="url" class="form-control" placeholder="URL" >  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input id="sortOrder" class="form-control" placeholder="排序" >  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
								<input type="file" id="addFile" name="addFile" />
					            </td>
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input id="yesNo" type="checkbox" class="icheckbox_square-blue" >
		      		</td>
		      	</tr>  	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="addContent()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 编辑窗口修改 -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="">广告编辑</h3>
		</div>
		<div class="modal-body">
			<input type="hidden" id="id">
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select id="cateShowUp" class="form-control" >
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input id="titleUp" class="form-control" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input id="urlUp" class="form-control" placeholder="URL" >  </td>
		      	</tr>
		      	<tr>
		      		<td>排序</td>
		      		<td><input id="sortOrderUp"  class="form-control" placeholder="排序" >  </td>
		      	</tr>
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<input type="file" id="updateFile" name="updateFile" />
								</td>
								<td>
									<img id="upImg" width="200px" height="100px">
								</td>
							</tr>
						</table>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input id="yesNoUp" type="checkbox" class="icheckbox_square-blue" >
		      		</td>
		      	</tr>
			 </table>

		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" onclick="updateContent()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

</body>
<script>
	function updateContent(){
		var i = '0'
		if ($("#yesNoUp").get(0).checked) {
			i = '1'
		}
		$.ajax({
			url: "/content/updateContent",
			data: {id:$("#id").val(),categoryId:$("#cateShowUp").val(),title:$("#titleUp").val(),
				url:$("#urlUp").val(),pic:$("#pic").val(),sortOrder:$("#sortOrderUp").val(),status:i},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					alert(result.message)
					window.location.reload()
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function toUpdate(id){
		$.ajax({
			url: "/content/toUpdate",
			data: {id:id},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					var cont = result.data
					$("#cateShowUp").val(cont.categoryId)
					$("#id").val(cont.id)
					$("#titleUp").val(cont.title)
					$("#sortOrderUp").val(cont.sortOrder)
					$("#urlUp").val(cont.url)
					$("#updateFile").attr("url",cont.pic)
					if(cont.status == 1){
						$("#yesNoUp").attr("checked",true)
					}
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function addContent(){
		var i = '0'
		if ($("#yesNo").get(0).checked) {
			i = '1'
		}
		$.ajax({
			url: "/content/addContent",
			data: {categoryId:$("#cateShow").val(),title:$("#title").val(),
					url:$("#url").val(),pic:$("#pic").val(),sortOrder:$("#sortOrder").val(),status:i},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					alert(result.message)
					window.location.reload()
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}

	$(function (){
		//文件上传
		$('#addFile').fileinput({//获取input的id
			language: 'zh',//需要依赖和插件
			uploadUrl: "/content/upload",//上传路径
			showCaption: true,
			showUpload: true,
			showRemove: true,
			showClose: true,
			allowedFileExtensions: ['jpg', 'png', 'gif', 'jfif', 'txt', 'pdf', 'xls', 'xlsx'],//可以上传的文件后缀名
			maxFileSize: 40960, //设置文件上传单位kb
			maxFileCount: 5, //最大上传数量
			browseClass: 'btn btn-primary', //按钮样式
			entryType: "multipart/form-data",//文件上传
		});
		//上传时，name属性回填给文本框赋值，添加到数据库中
		//上传成功后的回调方法
		$('#addFile').on('fileuploaded', function (event, data, previewId, index) {
			if (data.response.code == 666){
				$("#pic").val(data.response.data);//data.response就是返回的信息，data是里面的值
			}
		})
		//xiugai
		//文件上传
		$('#updateFile').fileinput({//获取input的id
			language: 'zh',//需要依赖和插件
			uploadUrl: "/content/uploadUp",//上传路径
			showCaption: true,
			showUpload: true,
			showRemove: true,
			showClose: true,
			allowedFileExtensions: ['jpg', 'png', 'gif', 'jfif', 'txt', 'pdf', 'xls', 'xlsx'],//可以上传的文件后缀名
			maxFileSize: 40960, //设置文件上传单位kb
			maxFileCount: 5, //最大上传数量
			browseClass: 'btn btn-primary', //按钮样式
			entryType: "multipart/form-data",//文件上传
		});
		//上传时，name属性回填给文本框赋值，添加到数据库中
		//上传成功后的回调方法
		$('#updateFile').on('fileuploaded', function (event, data, previewId, index) {
			if (data.response.code == 666){
				$("#pic").val(data.response.data);//data.response就是返回的信息，data是里面的值
			}
		})
		listContent()
		listCategroys()
	})
	function listCategroys(){
		$.ajax({
			url: "/content/getCategroys",
			data: {},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					var html = ''
					var cateList = result.data
					$(cateList).each(function (i,e){
						html += '<option value="'+ e.id +'">'
						html += e.name
						html += '</option>'
					})
					$("#cateShow").html(html)
					$("#cateShowUp").html(html)
				}else{
					$("#cateShow").html("<option>" + result.message + "</option>");
					$("#cateShowUp").html("<option>" + result.message + "</option>");
				}

			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function listContent(num){
		$.ajax({
			url: "/content/listContent",
			data: {pageNum:num},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == "666"){
					var content = result.data.rows
					var html = ""
					for (var i = 0; i < content.length; i++) {
						html += '<tr>'
						html += '<td><input value="'+ content[i].id +'" type="checkbox"></td>'
						html += '<td>'+ content[i].id +'</td>'
						html += '<td>'+ content[i].categoryId +'</td>'
						html += '<td>'+ content[i].title +'</td>'
						html += '<td>'+ content[i].url +'</td>'
						html += '<td>'
						html += '<img alt="" src="'+ content[i].pic +'" width="100px" height="50px">'
						html += '</td>'
						html += '<td>'+ content[i].sortOrder +'</td>'
						var status = '无效'
						if(content[i].status == 1){
							status = '有效'
						}
						html += '<td>'+ status +'</td>'
						html += '<td className="text-center">'
						html += '<button type="button" className="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate('+ content[i].id +')" data-target="#updateModal">修改</button>'
						html += '</td>'
						html += '</tr>'
					}
					$("#contentShow").html(html);
					// 分页
					$("#pageNum").val(result.data.pageNum);
					$("#pages").val(result.data.pages);
					$("#total").val(result.data.total);
				}else{
					$("#contentShow").html("<tr><td>" + result.message + "</td></td></tr>");
				}

			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	//批量删除方法
	function deleteConts() {
		if(confirm("是否删除")){
			var ids = []  //定义一个数组
			$("[type=checkbox]").each(function (i, e) {   //获取所有复选框的value,循环
				if ($(e).prop("checked")) {   //判断是否被选中
					ids.push($(e).val())  //选中就添加到数组中
				}
			})
			$.ajax({
				url: "/content/deleteConts?ids=" + ids,
				type: "get",
				dataType: "json",
				async: false,
				success: function (result) {
					if (result.code == "666") {
						alert(result.message)
						listContent()
					} else {
						alert(result.message)
					}
				},
				error: function (){
					alert("系统异常")
				}
			})
		}
	}
	//分页
	function toPage(id){
		var num = parseInt($("#pageNum").val());
		var pages = parseInt($("#pages").val());
		var p = num;
		if (id == 1) {
			p = 1;
			$("#pageNum").val(1);
		}
		if (id == 2){
			if (num == 1) {
				alert("已经到达首页了");
				return;
			}
			p = num - 1;
			$("#pageNum").val(num - 1);
		}
		if (id == 3){
			if (num == pages) {
				alert("已经到达尾页了");
				return;
			}
			p = num + 1;
			$("#pageNum").val(num + 1);
		}
		if (id==4){
			p = pages;
			$("#pageNum").val(pages);
		}
		listContent(p);
	}
</script>
</html>